package com.ymitcloud.module.pay.controller.admin.transfer.vo;


import static com.ymitcloud.framework.common.exception.enums.GlobalErrorCodeConstants.NOT_IMPLEMENTED;
import static com.ymitcloud.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.ymitcloud.module.pay.enums.transfer.PayTransferTypeEnum.typeOf;

import java.util.Map;


import com.ymitcloud.framework.common.util.validation.ValidationUtils;
import com.ymitcloud.framework.common.validation.InEnum;
import com.ymitcloud.framework.pay.core.enums.channel.PayChannelEnum;
import com.ymitcloud.module.pay.enums.transfer.PayTransferTypeEnum;

import com.ymitcloud.module.pay.enums.transfer.PayTransferTypeEnum.Alipay;
import com.ymitcloud.module.pay.enums.transfer.PayTransferTypeEnum.WxPay;

import jakarta.validation.Validator;
import jakarta.validation.constraints.AssertTrue;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.Data;

/** 管理后台 - 发起转账 Request VO */
@Data
public class PayTransferCreateReqVO {

    /** 应用编号*/
    @NotNull(message = "应用编号不能为空")
    private Long appId;

    /** 商户转账单编号*/
    @NotNull(message = "商户转账单编号不能为空")
    private String merchantTransferId;

    /** 转账类型*/

    @NotNull(message = "转账类型不能为空")
    @InEnum(PayTransferTypeEnum.class)
    private Integer type;


    /** 转账渠道*/

    @NotEmpty(message = "转账渠道不能为空")
    private String channelCode;

    @Min(value = 1, message = "转账金额必须大于零")
    @NotNull(message = "转账金额不能为空")
    private Integer price;


    /** 转账标题*/
    @NotEmpty(message = "转账标题不能为空")
    private String subject;

    /** 收款人姓名*/
    @NotBlank(message = "收款人姓名不能为空", groups = {Alipay.class})
    private String userName;

    /** 支付宝登录号*/
    @NotBlank(message = "支付宝登录号不能为空", groups = {Alipay.class})
    private String alipayLogonId;

    /** 微信 openId*/
    @NotBlank(message = "微信 openId 不能为空", groups = {WxPay.class})
    private String openid;

    /** 
     * 转账渠道的额外参数
     */

    private Map<String, String> channelExtras;

    public void validate(Validator validator) {
       PayTransferTypeEnum transferType = typeOf(type);
        switch (transferType) {
            case ALIPAY_BALANCE: {
                ValidationUtils.validate(validator, this, Alipay.class);
                break;
            }
            case WX_BALANCE: {
                ValidationUtils.validate(validator, this, WxPay.class);
                break;
            }
            default: {
                throw new UnsupportedOperationException("待实现");
            }
        }
    }

    @AssertTrue(message = "转账类型和转账渠道不匹配")
    public boolean isValidChannelCode() {
        PayTransferTypeEnum transferType = typeOf(type);
        switch (transferType) {
            case ALIPAY_BALANCE: {
                return PayChannelEnum.isAlipay(channelCode);
            }
            case WX_BALANCE:
            case BANK_CARD:
            case WALLET_BALANCE: {
                throw exception(NOT_IMPLEMENTED);
            }
        }
        return Boolean.FALSE;
    }

}
